package com.lanou.myim.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.RelativeLayout;

import com.easemob.EMValueCallBack;
import com.easemob.chat.EMGroup;
import com.easemob.chat.EMGroupManager;
import com.lanou.myim.R;
import com.lanou.myim.adapter.GroupListviewAdapter;

import com.lanou.myim.entity.MsgFromEty;

import java.util.List;


/**
 * Created by dllo on 16/3/9.
 */
public class GroupActivity extends BaseActivity implements View.OnClickListener {
    private RelativeLayout relativeLayout;
    private ListView listView;
    private GroupListviewAdapter adapter;
    private List<EMGroup> grouplist;
    private Handler handler;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_group_chat);
        relativeLayout = (RelativeLayout) findViewById(R.id.linkman_apply);
        relativeLayout.setOnClickListener(this);
        listView = (ListView) findViewById(R.id.lv_group);
        handler = new Handler(new Handler.Callback() {
            @Override
            public boolean handleMessage(Message message) {
                adapter = new GroupListviewAdapter(getApplicationContext(), grouplist);
                listView.setAdapter(adapter);
                return false;
            }
        });


        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
                Intent intent = new Intent();
                Bundle bundle = new Bundle();
                bundle.putSerializable("ID",new MsgFromEty("GROUP",null
                        ,grouplist.get(position).getGroupName(),grouplist.get(position).getGroupId()));
                intent.setClass(GroupActivity.this, PrivateChatActivity.class);
                intent.putExtras(bundle);
                startActivity(intent);
            }
        });



        EMGroupManager.getInstance().asyncGetGroupsFromServer(new EMValueCallBack<List<EMGroup>>() {
            @Override
            public void onSuccess(List<EMGroup> value) {
                grouplist = value;
                handler.sendEmptyMessage(0);
                Log.d("查看群id", value.get(0).getGroupId());
                Log.d("查看群名",value.get(0).getGroupName());
            }

            @Override
            public void onError(int i, String s) {

            }
        });

    }

    @Override
    public void onClick(View view) {
        Intent intent = new Intent(GroupActivity.this, AddGroupActivity.class);
        startActivity(intent);
    }
}
